<template>
  <div class="container">
    <div class="top-nav">
      <div @click="tab = 1" :class="{ 'nav-cell': true, active: tab === 1 }">
        <span>类别优惠券</span>
      </div>
      <div @click="tab = 2" :class="{ 'nav-cell': true, active: tab === 2 }">
        <span>商品优惠券</span>
      </div>
    </div>
    <div class="coupon-list" v-if="tab === 1">
      <coupon-cell
        v-for="item in classCouponList"
        :item="item"
        @getSucc="getSucc"
      ></coupon-cell>
      <div class="no-coupon" v-if="!classCouponList.length">
        <img src="@/assets/待入账收益-assets/no_message.png" alt="" />
        <div class="text">暂无优惠券</div>
      </div>
    </div>
    <div class="coupon-list" v-else>
      <div v-for="item in goodsCouponList" class="item-coupon-cell">
        <div class="img">
          <img :src="item.commodity.img_show" alt="" />
        </div>
        <div class="item-info">
          <div class="item-title">
            {{ item.commodity.name }}
          </div>
          <div class="coupon-value">{{ item.minus }}元优惠券</div>
        </div>
        <button @click="getGoodsCoupon(item.id)">立即领取</button>
      </div>
      <div class="no-coupon" v-if="!goodsCouponList.length">
        <img src="@/assets/待入账收益-assets/no_message.png" alt="" />
        <div class="text">暂无优惠券</div>
      </div>
    </div>
  </div>
</template>

<script>
import couponCell from "@/components/coupon-cell";
import { getCouponList, getCoupon } from "@/api/mall";

export default {
  data() {
    return {
      tab: 1,
      classCouponList: [],
      goodsCouponList: []
    };
  },
  methods: {
    getSucc(id) {
      this.classCouponList.forEach((item, index) => {
        if (item.id === id) {
          this.classCouponList.splice(index, 1);
          return false;
        }
      });
    },
    getGoodsCoupon(id) {
      getCoupon({ id }).then(res => {
        if (res.status === 200) {
          this.$dialog.toast({
            mes: res.data.data.message,
            timeout: 1500
          });
          this.goodsCouponList.forEach((item, index) => {
            if (item.id === id) {
              this.goodsCouponList.splice(index, 1);
              return false;
            }
          });
        }
      });
    }
  },
  created() {
    getCouponList().then(res => {
      if (res.status === 200) {
        this.classCouponList = res.data.data.classify;
        this.goodsCouponList = res.data.data.commodity;
      }
    });
  },
  components: {
    couponCell
  }
};
</script>

<style lang="less" scoped>
.container {
  min-height: 100%;
  height: auto;

  .top-nav {
    background: #fff;
    height: 1.76rem;
    display: flex;
    justify-content: center;
    color: rgba(68, 68, 68, 1);
    font-size: 0.58rem;

    .nav-cell {
      width: 3.5rem;
      text-align: center;
      margin: 0.5rem 1.34rem;
      transition-duration: 0.1s;

      span {
        transition-duration: 0.2s;
        display: inline-block;
        height: 0.8rem;
      }
    }

    .active {
      font-weight: bold;
      font-size: 0.64rem;

      span {
        border-bottom: 0.24rem solid rgba(0, 120, 255, 1);
      }
    }
  }

  .coupon-list {
    margin-top: 0.48rem;
    padding: 0 3.2%;

    .item-coupon-cell {
      height: 3.8rem;
      background-image: url("../../assets/商品优惠券-assets/bg.png");
      background-size: 100% 100%;
      background-repeat: no-repeat;
      margin-bottom: 0.24rem;
      position: relative;
      overflow: hidden;
      display: flex;
      padding: 0.32rem;

      .img {
        height: 3.2rem;
        width: 3.2rem;
        background-color: #dfdfdf;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .item-info {
        margin-left: 0.8rem;
        width: 50%;

        .item-title {
          color: rgba(68, 68, 68, 1);
          font-size: 0.58rem;
          margin: 0.1rem 0 0.2rem;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          white-space: normal;
        }

        .coupon-value {
          font-weight: bold;
          color: rgba(248, 88, 89, 1);
          font-size: 0.64rem;
        }
      }

      button {
        position: absolute;
        background: rgba(248, 88, 89, 1);
        height: 1rem;
        line-height: 1rem;
        border-radius: 0.5rem;
        color: #fff;
        font-size: 0.48rem;
        border: none;
        padding: 0 0.24rem;
        right: 2.2%;
        top: 1.4rem;
      }
    }
  }

  .no-coupon {
    text-align: center;
    padding-top: 3rem;

    img {
      height: 4rem;
    }

    .text {
      color: rgba(68, 68, 68, 1);
      font-size: 0.58rem;
    }
  }
}
</style>
